package cn.jhh.content.controller;

import cn.jhh.content.model.dto.AddCourseDto;
import cn.jhh.content.model.dto.CourseBaseInfoDto;
import cn.jhh.content.model.dto.UpdateCourseDto;
import cn.jhh.content.model.entity.CourseBase;
import cn.jhh.content.service.CourseBaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author jhh
 * @since 2025-05-23
 */
@Api(value = "课程管理接口", tags = "课程管理接口")
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * 分页查询课程列表
     * 
     * @param pageNum 当前页码
     * @param pageSize 每页记录数
     * @param courseName 课程名称(可选)
     * @param auditStatus 审核状态(可选)
     * @param companyId 机构ID(可选)
     * @return 分页课程数据
     */
    @ApiOperation("分页查询课程")
    @GetMapping("/list")
    public Map<String, Object> list(
            @ApiParam(value = "当前页码", required = true) @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("课程名称") @RequestParam(required = false) String courseName,
            @ApiParam("审核状态") @RequestParam(required = false) String auditStatus,
            @ApiParam("机构ID") @RequestParam(required = false) Long companyId) {
        
        // 创建分页对象
        Page<CourseBase> page = new Page<>(pageNum, pageSize);
        
        // 构建查询条件
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.isNotEmpty(courseName)) {
            wrapper.like(CourseBase::getName, courseName);
        }
        
        if (StringUtils.isNotEmpty(auditStatus)) {
            wrapper.eq(CourseBase::getAuditStatus, auditStatus);
        }
        
        if (companyId != null) {
            wrapper.eq(CourseBase::getCompanyId, companyId);
        }
        
        // 按创建时间降序排序
        wrapper.orderByDesc(CourseBase::getCreateDate);
        
        // 执行查询
        Page<CourseBase> result = courseBaseService.page(page, wrapper);
        
        // 封装返回结果
        Map<String, Object> map = new HashMap<>();
        map.put("total", result.getTotal());
        map.put("records", result.getRecords());
        map.put("current", result.getCurrent());
        map.put("size", result.getSize());
        map.put("pages", result.getPages());
        
        return map;
    }
    
    /**
     * 根据ID获取课程详情
     * 
     * @param id 课程ID
     * @return 课程详情
     */
    @ApiOperation("获取课程详情")
    @GetMapping("/{id}")
    public CourseBase getById(@ApiParam(value = "课程ID", required = true) @PathVariable("id") Long id) {
        return courseBaseService.getById(id);
    }
    
  
    @ApiOperation("创建课程")
    @PostMapping
    public CourseBaseInfoDto createCourseBase(@ApiParam(value = "课程信息", required = true) @RequestBody AddCourseDto addCourseDto) {
        //M老师
        Long companyId = 1232141425L;
        return courseBaseService.createCourseBase(companyId,addCourseDto);
    }
    

    @ApiOperation("更新课程")
    @PutMapping("/{id}")
    public Map<String, Object> update(
            @ApiParam(value = "课程ID", required = true) @PathVariable("id") Long id,
            @ApiParam(value = "课程信息", required = true) @RequestBody UpdateCourseDto dto) {
        if (!id.equals(dto.getId())) {
            throw new IllegalArgumentException("路径ID与课程ID不匹配");
        }
        return courseBaseService.updateCourseBaseInfo(dto);
    }
    
    /**
     * 删除课程
     * 
     * @param id 课程ID
     * @return 删除结果
     */
    @ApiOperation("删除课程")
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@ApiParam(value = "课程ID", required = true) @PathVariable("id") Long id) {
        Map<String, Object> result = new HashMap<>();
        
        // 删除课程
        boolean success = courseBaseService.removeById(id);
        
        if (success) {
            result.put("success", true);
            result.put("message", "课程删除成功");
        } else {
            result.put("success", false);
            result.put("message", "课程删除失败");
        }
        
        return result;
    }
    
    /**
     * 修改课程审核状态
     * 
     * @param id 课程ID
     * @param auditStatus 审核状态
     * @return 更新结果
     */
    @ApiOperation("修改课程审核状态")
    @PutMapping("/{id}/audit/{auditStatus}")
    public Map<String, Object> updateAuditStatus(
            @ApiParam(value = "课程ID", required = true) @PathVariable("id") Long id,
            @ApiParam(value = "审核状态", required = true) @PathVariable("auditStatus") String auditStatus) {

        Map<String, Object> result = new HashMap<>();

        // 获取课程
        CourseBase courseBase = courseBaseService.getById(id);

        if (courseBase == null) {
            result.put("success", false);
            result.put("message", "课程不存在");
            return result;
        }

        // 更新审核状态
        courseBase.setAuditStatus(auditStatus);
        courseBase.setChangeDate(LocalDateTime.now());

        boolean success = courseBaseService.updateById(courseBase);

        if (success) {
            result.put("success", true);
            result.put("message", "审核状态更新成功");
        } else {
            result.put("success", false);
            result.put("message", "审核状态更新失败");
        }

        return result;
    }
}
